package com.beehoo.dcportal.admin.action;

import java.util.List;

import com.beehoo.dcportal.admin.service.CommunityService;
import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.base.BaseAction;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.CommunityBean;
import com.beehoo.dcportal.model.FileBean;

public class CommunityAction extends BaseAction {

	private static final long serialVersionUID = -6180373342335805213L;
	private CommunityService communityService;
	private List<CommunityBean> resultList;
	private CommunityBean communityBean;
	private String communityId;
	private FileBean fileBean;

	public List<CommunityBean> getResultList() {
		return resultList;
	}

	public void setResultList(List<CommunityBean> resultList) {
		this.resultList = resultList;
	}

	public CommunityBean getCommunityBean() {
		return communityBean;
	}

	public void setCommunityBean(CommunityBean communityBean) {
		this.communityBean = communityBean;
	}

	public String getCommunityId() {
		return communityId;
	}

	public void setCommunityId(String communityId) {
		this.communityId = communityId;
	}

	public FileBean getFileBean() {
		return fileBean;
	}

	public void setFileBean(FileBean fileBean) {
		this.fileBean = fileBean;
	}

	public void setCommunityService(CommunityService communityService) {
		this.communityService = communityService;
	}

	public String list(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			resultList = communityService.queryCommunities(streetId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return SUCCESS;
	}
	

	public String initUpdate(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		try {
			communityBean = communityService.queryCommunity(streetId, communityId);
			super.getRequest().setAttribute("method", "update");
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return SUCCESS;
		}
		return INPUT;
	}
	
	public String update(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		if(communityBean != null){
			communityBean.setStreetId(streetId);
			communityBean.setFileBean(fileBean);
			communityBean.setPhotoPath(super.getText("custom.community.img.path"));
			communityBean.setPhotoDir(super.getText("custom.community.img.dir"));
		}
		try {
			communityService.updateCommunity(communityBean);
		} catch (BaseException e) {
			super.getRequest().setAttribute("method", "update");
			setMessage(super.getText(e.getMessage()));
			return INPUT;
		}
		return list();
	}
	
	public String add(){
		String streetId = (String) getSession().get(Constant.SESSION_STREET_ID);
		if(communityBean != null){
			communityBean.setStreetId(streetId);
			communityBean.setFileBean(fileBean);
			communityBean.setPhotoPath(super.getText("custom.community.img.path"));
			communityBean.setPhotoDir(super.getText("custom.community.img.dir"));
		}
		try {
			communityService.addCommunity(communityBean);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return INPUT;
		}
		return list();
	}
	
	public String del(){
		try {
			communityService.delCommunity(communityId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return list();
	}
}
